﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using Printer;
using Utils.ZPLPrinter;
using OpenClientX.Utils;
using System.Drawing.Printing;
using System.Drawing;

namespace activeX01
{
    //[Guid("3509DA26-0480-4CB5-B791-15FB6C1B7FAC")]
    [Guid("B71CF154-B287-4F99-97E1-CE99E1ECCF0F")]
    public partial class UserControl1 : UserControl, IObjectSafety
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择条码打印机
        /// </summary>
        /// <returns></returns>
        public string SelectPrinter()
        {
            SelectPrinter sp = new SelectPrinter();
            sp.ShowDialog();
            return "";
        }

        /// <summary>
        /// 打印条码
        /// </summary>
        /// <param name="BarCode"></param>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public String PrintBarCode(String BarCode,String serviceUrl)
        {
            //先清除原来的条码图像缓存
            CommontUtils.clearCache();
            try
            {
                if (!String.IsNullOrEmpty(serviceUrl))
                {
                    if (CommontUtils.UpgradeTag(serviceUrl))
                    {
                        MessageBox.Show("浏览器插件现在有新版本，在更新过程中，需要关闭当前页面，点击确定继续。", "提示信息", MessageBoxButtons.OK);
                        CommontUtils.DownloadUpgradeExe(serviceUrl);//下载更新文件
                        return "";
                    }
                }
                String fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                PrintDocument fPrintDocument = new PrintDocument();
                //MessageBox.Show(fPrintDocument.PrinterSettings.PrinterName);
                if (fPrintDocument.PrinterSettings.PrinterName.Contains("PC42t"))
                {
                    //MessageBox.Show("in PC42t");
                    BarCodeUtil.CreateBarcode(BarCode, fileName + ".bmp", true);
                    HoneywellPrintHelper.StartPrint("C://MssClientCache//barcodeCache//" + fileName + ".bmp", fPrintDocument.PrinterSettings.PrinterName);
                }
                else
                {
                    //MessageBox.Show("in Zebar");
                    BarCodeUtil.CreateBarcode(BarCode, fileName + ".png", false);
                    PrintService printService = new PrintService();
                    printService.StartPrint(new Bitmap("C://MssClientCache//barcodeCache//" + fileName + ".png") , "image");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return "yes";
        }

        /// <summary>
        /// 启动客户端的接口
        /// </summary>
        /// <param name="username"></param>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public String openCLient(String username,String serviceUrl)
        {
            //MessageBox.Show("username:" + username + ",serviceUrl:" + serviceUrl);
            if (!String.IsNullOrEmpty(serviceUrl))
            {
                if (CommontUtils.UpgradeTag(serviceUrl))
                {
                    MessageBox.Show("浏览器插件现在有新版本，在更新过程中，需要关闭当前页面，点击确定继续。","提示信息",MessageBoxButtons.OK);
                    CommontUtils.DownloadUpgradeExe(serviceUrl);//下载更新文件
                    return "";
                }
            }
            
            if (!File.Exists("C:\\MssClient\\MSSClient.exe"))
            {
                return "noFile";
            }
            string str1 = username+ ",NoPassWord";
            File.WriteAllText(@"C://openClient.txt", str1);
            Process p = Process.Start("C:\\MssClient\\MSSClient.exe");
            return "";
        }

        /// <summary>
        /// 启动客户端，进行补单获取
        /// </summary>
        /// <param name="username"></param>
        /// <param name="serviceUrl"></param>
        /// <returns></returns>
        public String getBudanInfo(String username,String serviceUrl)
        {
            if (!String.IsNullOrEmpty(serviceUrl))
            {
                if (CommontUtils.UpgradeTag(serviceUrl))
                {
                    MessageBox.Show("浏览器插件现在有新版本，在更新过程中，需要关闭当前页面，点击确定继续。", "提示信息", MessageBoxButtons.OK);
                    CommontUtils.DownloadUpgradeExe(serviceUrl);//下载更新文件
                    return "";
                }
            }
            if (!File.Exists("C:\\MssClient\\MSSClient.exe"))
                return "noExeFile";

            string strOenCLient = username + ",NoPassWord";
            File.WriteAllText(@"C://MssClientCache//BudanInfoGet.txt", " ");
            File.WriteAllText(@"C://openClient.txt", strOenCLient);
            Process p = Process.Start("C:\\MssClient\\MSSClient.exe");
            return "";
        }

        #region IObjectSafety 接口成员实现

        private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
        private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
        private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
        private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
        private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

        private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
        private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
        private const int S_OK = 0;
        private const int E_FAIL = unchecked((int)0x80004005);
        private const int E_NOINTERFACE = unchecked((int)0x80004002);

        private bool _fSafeForScripting = true;
        private bool _fSafeForInitializing = true;

        public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
        {
            int Rslt = E_FAIL;

            string strGUID = riid.ToString("B");
            pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
            switch (strGUID)
            {
                case _IID_IDispatch:
                case _IID_IDispatchEx:
                    Rslt = S_OK;
                    pdwEnabledOptions = 0;
                    if (_fSafeForScripting == true)
                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
                    break;
                case _IID_IPersistStorage:
                case _IID_IPersistStream:
                case _IID_IPersistPropertyBag:
                    Rslt = S_OK;
                    pdwEnabledOptions = 0;
                    if (_fSafeForInitializing == true)
                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
                    break;
                default:
                    Rslt = E_NOINTERFACE;
                    break;
            }

            return Rslt;
        }

        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
        {
            int Rslt = E_FAIL;
            string strGUID = riid.ToString("B");
            switch (strGUID)
            {
                case _IID_IDispatch:
                case _IID_IDispatchEx:
                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
                        Rslt = S_OK;
                    break;
                case _IID_IPersistStorage:
                case _IID_IPersistStream:
                case _IID_IPersistPropertyBag:
                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
                        Rslt = S_OK;
                    break;
                default:
                    Rslt = E_NOINTERFACE;
                    break;
            }

            return Rslt;
        }

        #endregion
    }
}
